/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.model.dao;

import hibernate.pojo.Chitietdondathang;
import hibernate.pojo.Dondathang;
import hibernate.util.HibernateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author phathuynh
 */
public class ChiTietDonDatHangDAO {
    
    //<editor-fold defaultstate="collapsed" desc="1. Thêm chi tiết đơn đặt hàng">
    public static int addOrderDetail(Chitietdondathang order) {
      Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(order);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
      return 1;
   }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy mã đơn đặt hàng cuối cùng">
    public static int getLastOrderID(){
        int ddh = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "Select MAX(ddh.maDonDatHang) from Dondathang ddh";
            //Query query = session.createQuery(hql).setCacheable(true);
            Query query = session.createQuery(hql);
            Object objDdh = query.uniqueResult();
            ddh = (Integer)objDdh;
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return ddh;
    }
    
    public static Dondathang getLastOrder(int orderID){
        Dondathang ddh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            ddh = (Dondathang) session.get(Dondathang.class, orderID);
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return ddh;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="3. Lấy chi tiết đơn đh theo mã người dùng - Phiếu mua hàng online">
    public static List<Chitietdondathang> getOnlineOrderDetail(int maKhachHang){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            // FROM Chitietdondathang ctddh inner join fetch ctddh.dondathang
            String hql = "From Chitietdondathang ctddh Where ctddh.dondathang.khachhang.maKhachHang=:maKhachHang "
                    + "and (ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=1 or ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=3)";
            Query query = session.createQuery(hql);
            query.setInteger("maKhachHang", maKhachHang);
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="3b. Tìm chi tiết đơn đh theo mã người dùng + mã ctdđh- Tim Phiếu mua hàng online">
    public static List<Chitietdondathang> searchOnlineOrderDetail(int maKhachHang, int maChiTietDonDatHang){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            // FROM Chitietdondathang ctddh inner join fetch ctddh.dondathang
            String hql = "From Chitietdondathang ctddh Where ctddh.maChiTietDonDatHang=:maChiTietDonDatHang and ctddh.dondathang.khachhang.maKhachHang=:maKhachHang "
                    + "and (ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=1 or ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=3)";
            Query query = session.createQuery(hql);
            query.setInteger("maChiTietDonDatHang", maChiTietDonDatHang);
            query.setInteger("maKhachHang", maKhachHang);
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4. Lấy chi tiết đơn đh theo mã người dùng - Phiếu giao hàng online">
    public static List<Chitietdondathang> getDeliveryOrderDetail(int maKhachHang){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            // FROM Chitietdondathang ctddh inner join fetch ctddh.dondathang
            String hql = "From Chitietdondathang ctddh Where ctddh.dondathang.khachhang.maKhachHang=:maKhachHang "
                    + "and ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=2";
            Query query = session.createQuery(hql);
            query.setInteger("maKhachHang", maKhachHang);
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="5. Lấy chi tiết đơn đh theo mã người dùng - Lịch sử mua hàng">
    public static List<Chitietdondathang> getHistoricalOrderDetail(int maKhachHang){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            // FROM Chitietdondathang ctddh inner join fetch ctddh.dondathang
            String hql = "From Chitietdondathang ctddh Where ctddh.dondathang.khachhang.maKhachHang=:maKhachHang";
            Query query = session.createQuery(hql);
            query.setInteger("maKhachHang", maKhachHang);
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    
    //</editor-fold>
    
     //<editor-fold defaultstate="collapsed" desc="6. Search Transaction">
    
    public static List<Chitietdondathang> SearchTransaction(int maKhachHang, String ngayTu, String ngayDen){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = 
                "From Chitietdondathang ctddh Where ctddh.dondathang.ngayDat>=:ngayDat "
                    + "and ctddh.dondathang.ngayDat<=:ngayDat "
                    + "and ctddh.dondathang.khachhang.maKhachHang=:maKhachHang ";
            Query query = session.createQuery(hql);
            if(maKhachHang!=-1){
                query.setInteger("maKhachHang", maKhachHang);
            }
            if(!ngayTu.equals("")){
                
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Date d = (Date) sdf.parse(ngayTu);
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dd = sdf.format(d);
                //query.setString("ngayDat", "'"+dd+"'");
                query.setString("ngayDat", dd);
            }
            if(!ngayDen.equals("")){
                
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Date d = (Date) sdf.parse(ngayDen);
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dd = sdf.format(d);
                //query.setString("ngayDat", "'"+dd+"'");
                query.setString("ngayDat", dd);
            }
            
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        } catch (ParseException ex) {
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="SearchDelivery">
    
    public static List<Chitietdondathang> SearchDelivery(int maKhachHang, String ngayDat){
        List<Chitietdondathang> lstOrderDetail = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = 
                    "From Chitietdondathang ctddh Where ctddh.dondathang.ngayDat=:ngayDat "
                    + "and ctddh.dondathang.khachhang.maKhachHang=:maKhachHang "
                    + "and ctddh.dondathang.loaithanhtoan.maLoaiThanhToan=2";
            Query query = session.createQuery(hql);
            if(maKhachHang!=-1){
                query.setInteger("maKhachHang", maKhachHang);
            }
            if(!ngayDat.equals("")){
                
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Date d = (Date) sdf.parse(ngayDat);
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dd = sdf.format(d);
                
                //query.setString("ngayDat", "'"+dd+"'");
                query.setString("ngayDat", dd);
            }
            
            lstOrderDetail = (List<Chitietdondathang>) query.list();
        } catch (ParseException ex) {
        }catch(HibernateException hex){
            System.err.print(hex);
        }finally{
            session.close();
        }
        return lstOrderDetail;
    }
    //</editor-fold>
    
}
